﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.Regions;
using TimeSplicerModule.DomainModels;
using TimeSplicerModule.ViewModels;
using TimeSplicerModule.Factories;
using TimeSplicerModule.Controllers;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using TimeSplicerModule.Services;

namespace TimeSplicerModule.Views
{
    /// <summary>
    /// Interaction logic for DepartmentView.xaml
    /// </summary>
    public partial class TeamView : UserControl, INavigationAware
    {
        private TeamViewModel Model;
        private readonly IUnityContainer _container;
        private readonly ITeamService _teamService;
        private readonly ITeamController _teamController;

        public TeamView(IUnityContainer container, ITeamService teamService, ITeamController teamController)
        {
            InitializeComponent();

            _container = container;
            _teamService = teamService;
            _teamController = teamController;
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false; //throw new NotImplementedException();
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            //throw new NotImplementedException();
        }

        public async void OnNavigatedTo(NavigationContext navigationContext)
        {
            if (navigationContext.Parameters.Count() == 0)
            {
                await Task.Run(() => Model = _teamService.CreateNew());
            }
            else
            {
                var guid = navigationContext.Parameters["teamGuid"].ToString();
                Guid result;

                if (Guid.TryParse(guid, out result))
                {
                    await Task.Run(() => Model = _teamService.Get(result));
                }
            }


            this.DataContext = Model;
        }

        private async void saveButton_Click(object sender, RoutedEventArgs e)
        {
            this.saveButton.IsEnabled = false;
            this.saveExitButton.IsEnabled = false;
            this.cancelButton.IsEnabled = false;

            await Task.Run(() => Model = _teamService.Save(Model));

            this.saveButton.IsEnabled = true;
            this.saveExitButton.IsEnabled = true;
            this.cancelButton.IsEnabled = true;
        }

        private void saveExitButton_Click(object sender, RoutedEventArgs e)
        {
            saveButton_Click(sender, e);
            cancelButton_Click(sender, e);
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            // go back
            _teamController.NavigateToListView();
        }
    }
}
